package priv.bigant.intranet.visual.win;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import priv.bigant.intrance.common.log.LogUtil;
import priv.bigant.intranet.client.ClientConfig;
import priv.bigant.intranet.client.Domain;
import priv.bigant.intranet.client.ex.ServerConnectException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainWin extends JFrame {
    private JButton showdownBtn;
    private JPanel con;
    private JTextArea console;
    private JPanel consolePlane;
    private JButton clearLogBtn;
    private JComboBox<String> logLevelBox;
    private JButton configBtn;
    private final ClientConfig clientConfig = ClientConfig.getClientConfig();
    private final JDialog configWin;
    private final Domain domain = new Domain();
    private static final Logger LOG = LogUtil.getLog();


    public MainWin() {
        setContentPane(con);
        init();
        this.configWin = new ConfigWin(this, clientConfig);
        domain.setReturnError(x -> showdown());
    }

    public void initConnectBtn() {
        showdownBtn.addActionListener(x -> {
            if (showdownBtn.getText().equals("链接")) {
                connect();
            } else if (showdownBtn.getText().equals("断开")) {
                showdown();
            }
        });
    }

    public void init() {
        getContentPane().setBackground(Color.black);
        setBounds(60, 100, 1000, 1000);//设置位置大小
        setVisible(true);//可视性设置
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置按x后的操作.[这个是关闭整个程序,将会关闭所有窗口]
        initLog();
        initConnectBtn();
        initConfigBtn();
    }

    public void initConfigBtn() {
        configBtn.addActionListener(x -> {
            configWin.setVisible(true);
        });
    }

    public void initLog() {
        console = new JTextArea();
        console.setEditable(false);
        JScrollPane scroll = new JScrollPane(console);
        consolePlane.add(scroll);
        console.setLineWrap(true);
        ConsoleHandler.init(console, scroll);

        logLevelBox.setSelectedItem("一般");

        logLevelBox.addItemListener(e -> {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                setLogLevel();
            }
        });

        clearLogBtn.addActionListener(e -> console.setText(""));

    }


    private void setLogLevel() {
        String selectLevel = Objects.requireNonNull(logLevelBox.getSelectedItem()).toString();
        Level level;
        switch (selectLevel) {
            case "严重":
                level = Level.SEVERE;
                break;
            case "警示":
                level = Level.WARNING;
                break;
            case "详细":
                level = Level.FINE;
                break;
            case "超详细":
                level = Level.FINER;
                break;
            case "最详细":
                level = Level.FINEST;
                break;
            default:
                level = Level.INFO;
        }
        LOG.setLevel(level);
    }


    private void connect() {
        try {
            LOG.info("链接中");
            domain.connect();
            domain.startListener();
            showdownBtn.setText("断开");
            setTitle(clientConfig.getHostName());
        } catch (IOException | ServerConnectException e) {
            JOptionPane.showMessageDialog(con, e.getMessage(), "服务器链接失败", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void showdown() {
        domain.showdown();
        showdownBtn.setText("链接");
    }

    /**
     * 监听关口关闭时先断开链接
     *
     * @param e
     */
    @Override
    protected void processWindowEvent(WindowEvent e) {
        if (e.getID() == WindowEvent.WINDOW_CLOSING)
            showdown();
        super.processWindowEvent(e); //该语句会执行窗口事件的默认动作(如：隐藏)
    }


    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        con = new JPanel();
        con.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 5, new Insets(0, 0, 0, 0), -1, -1));
        con.add(panel1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, true));
        showdownBtn = new JButton();
        showdownBtn.setText("链接");
        panel1.add(showdownBtn, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label1 = new JLabel();
        label1.setText("日志级别");
        panel1.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        clearLogBtn = new JButton();
        clearLogBtn.setText("清空日志");
        panel1.add(clearLogBtn, new GridConstraints(0, 4, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        logLevelBox = new JComboBox();
        final DefaultComboBoxModel defaultComboBoxModel1 = new DefaultComboBoxModel();
        defaultComboBoxModel1.addElement("严重");
        defaultComboBoxModel1.addElement("警示");
        defaultComboBoxModel1.addElement("一般");
        defaultComboBoxModel1.addElement("详细");
        defaultComboBoxModel1.addElement("超详细");
        defaultComboBoxModel1.addElement("最详细");
        logLevelBox.setModel(defaultComboBoxModel1);
        panel1.add(logLevelBox, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        configBtn = new JButton();
        configBtn.setText("配置");
        panel1.add(configBtn, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        consolePlane = new JPanel();
        consolePlane.setLayout(new CardLayout(0, 0));
        con.add(consolePlane, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return con;
    }

    public Domain getDomain() {
        return domain;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("InsuranceWin");
        frame.setContentPane(new MainWin().con);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

